// pages/personal-information.js
// 获取全局应用程序实例对象
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 0,
    pic: '',
    phone: '',
    province: '',
    name:'',
    username: '',
    phone: '',
    birth: '',
    address: '',
    items: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.personalInfo.name){
      this.setData({
        name: app.globalData.personalInfo.name,
        items: [
          { name: '姓名', vlaue: app.globalData.personalInfo.name},
          { name: '电话', vlaue: app.globalData.personalInfo.phone || '' },
          { name: '生日', vlaue: app.globalData.personalInfo.birth || '' },
          { name: '地址', vlaue: app.globalData.personalInfo.address || '' }
        ]
      })
    }else{
      this.setData({
        name: app.globalData.personalInfo.username,
        items: [
          { name: '姓名', vlaue: app.globalData.personalInfo.username },
          { name: '电话', vlaue: app.globalData.personalInfo.phone || '' },
          { name: '生日', vlaue: app.globalData.personalInfo.birth || '' },
          { name: '地址', vlaue: app.globalData.personalInfo.address || '' }
        ]
      })
    }
    this.setData({
      money: parseInt(app.globalData.personalInfo.money) || 0,
      pic: app.globalData.personalInfo.pic || '',
      province: app.globalData.personalInfo.province || '',
      phone: app.globalData.personalInfo.phone || '',
      birth: app.globalData.personalInfo.birth || '',
      address: app.globalData.personalInfo.address || ''
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  toBalance: function () {
    wx.navigateTo({
      url: "/pages/balance/balance"
    })
  },
  toMemberInformation: function () {
    wx.navigateTo({
      url: "/pages/member-information/member-information"
    })
  },
  toMyorder: function (e) {
    wx.navigateTo({
      url: "/pages/myorder/myorder?id=" + e.currentTarget.dataset.id
    })
  },
  toCustomerService: function () {
    wx.navigateTo({
      url: "/pages/customer-service/customer-service?index=1"
    })
  },
  bindDateChange: function (e) {
    this.setData({
      birth: e.detail.value
    })
  },

  //报错
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 5000);
  },

  //模态框
  powerDrawer: function (e) {
    console.log(e)
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  formSubmit: function (e) {

    var that = this;
    var address = e.detail.value.address;
    var birth = e.detail.value.birth;
    var phone = e.detail.value.phone;
    var name = e.detail.value.name;
    var re = /^1\d{10}$/;    //正则表达式 手机号判断

    if (!re.test(phone)){
      setTimeout(() => {
        that.setData(
          { popErrorMsg: "手机号填写错误！" }
        );
        that.ohShitfadeOut();
        return;
      }, 100)
    }
    else if (address && birth && phone && name) {
      var params = {
        openid: app.globalData.info.openid,
        address: address,
        birth: birth,
        phone: phone,
        name: name
      };
      app.network.POST(
        'https://www.ausck.com/api/edit_info',
        {
          params: params,
          success: function (res) {
            if(res.data.status==200){
              that.setData({
                name: name,
                phone: phone,
                birth: birth,
                address: address,
                items: [
                  { name: '姓名', vlaue: name },
                  { name: '电话', vlaue: phone },
                  { name: '生日', vlaue: birth },
                  { name: '地址', vlaue: address }
                ]
              })
            }
          },
          fail: function () {
            //失败后的逻辑  
          },
        })
    }
    else{
      setTimeout(() => {
        that.setData(
          { popErrorMsg: "资料填写不完全！" }
        );
        that.ohShitfadeOut();
        return;
      }, 100)
    }

  },

  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  }


})


